package com.ssb.plugins;

import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.tools.StrUtil;

import java.util.List;
import java.util.Map;

/**
 * @description: 我的分佣插件
 * @author: Hou Dayu
 * @date: Created in 2020/3/29
 */
public class MyCommissionPluginImpl extends HheSuperPlugin {

    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String userId = String.valueOf(map.get("ssCommissionDetail.w_userId"));//用户Id
        if(StrUtil.isNull(userId) ){
            throw new BusinessException("当前渠道端信息不能为空");
        }
        String sysName = String.valueOf(map.get("ssCommissionDetail.w_sysName"));//批定查询周期
        StringBuilder sb = new StringBuilder("");
        if(StrUtil.isNull(sysName)) {
            sb.append("select scd.sys_name,count(info_id) policy_num, sum(premium) premium_num,sum(amount) amount_num, money_num from ss_Commission_Detail scd ");
            sb.append(" left join (select sys_name,sum(money) money_num from ss_cash_apply WHERE apply_status ='90' and apply_user_id='" + userId + "' group by sys_name) sca ");
            sb.append(" on scd.sys_name = sca.sys_name ");
            sb.append(" where user_id='" + userId + "'" );
            sb.append(" group by scd.sys_name order by scd.sys_name desc ");
            List list = select(sb.toString(),DATA_SOURCE);
            map.put(CustomMethodPlugin.pluginInnerReturn,list);
        } else {
            sb.append("select scd.sys_name,sum(amount) amount_num from ss_Commission_Detail scd ");
            sb.append(" where scd.user_id='" + userId + "'");
            sb.append(" and scd.sys_name='"+sysName+"'");
            Map result = selectInfo(sb.toString(),DATA_SOURCE);
            map.put(CustomMethodPlugin.pluginInnerReturn,result);
        }
        map.put(CustomMethodPlugin.resultMark, "T");
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
